package com.wang.openApigateway.inservice;

import com.wang.openApigateway.common.CommonConstant;
import com.wang.openApigateway.model.FilterRequestModel;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;

public class ConvertRequestModel {
    /**
     * 将请求头中的参数取出来转换为一个model，便于操作
     *
     * @param request
     * @param headers
     * @return
     */
    public FilterRequestModel convertModel(ServerHttpRequest request, HttpHeaders headers) {
        String path = CommonConstant.INTERFACE_HOST + request.getPath().value(); //最终请求地址
        String method = String.valueOf(request.getMethod()); //请求方法
        String remoteAddress = String.valueOf(request.getRemoteAddress()); //请求来源地址
        String sourceAddress = request.getURI().getHost(); //请求主机地址

        String accessKey = headers.getFirst("accessKey");
        String nonce = headers.getFirst("nonce");
        String timestamp = headers.getFirst("timestamp");
        String body = headers.getFirst("body");
        String sign = headers.getFirst("sign");
        return new FilterRequestModel(path, method, remoteAddress, sourceAddress, accessKey, nonce, timestamp, body, sign);
    }
}
